<template>
  <div id="manageQuestionView">
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #id="{ record }">
        {{ record.id.substring(record.id.length - 5) }}
      </template>
      <template #title="{ record }">
        {{ record.title }}
      </template>
      <template #content="{ record }">
        {{ record.content }}
      </template>
      <template #answer="{ record }">
        {{ record.answer }}
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag
            v-for="(tag, index) of JSON.parse(record.tags)"
            :key="index"
            color="cyan"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #judgeCase="{ record }">
        {{ record.judgeCase }}
      </template>
      <template #judgeConfig="{ record }">
        {{ record.judgeConfig }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD, HH:mm") }}
      </template>
      <template #updateTime="{ record }">
        {{ moment(record.updateTime).format("YYYY-MM-DD, HH:mm") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doUpdate(record)">修改</a-button>
          <a-button status="danger" @click="doDelete(record)">删除</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { Question, QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const router = useRouter();
const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  pageSize: 10,
  current: 1,
});

const onPageChange = (pageNumber: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: pageNumber,
  };
};

const doUpdate = async (question: Question) => {
  // 点击修改之后需要跳转到修改页面
  await router.push({
    path: "/update/question",
    query: {
      id: question.id,
    },
  });
};
const doDelete = async (question: Question) => {
  const result = await QuestionControllerService.deleteQuestionUsingPost({
    id: question.id,
  });
  if (result.code === 0) {
    message.success("题目删除成功");
    // 重新获取数据，更新页面
    await loadData();
  } else {
    message.error("删除题目失败," + result.message);
  }
};
const loadData = async () => {
  const result = await QuestionControllerService.listQuestionByPageUsingPost(
    searchParams.value
  );
  if (result.code === 0) {
    dataList.value = result.data.records;
    total.value = result.data.total;
  } else {
    message.error("加载失败，" + result.message);
  }
};

/**
 * watchEffect的作用：监听传入的方法里的所有的变量，一旦有一个值发生改变就会触发loadData()方法
 * 这里是监听 searchParams 变量，变量改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});
/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});
const show = ref(true);

const columns = [
  {
    title: "id",
    slotName: "id",
  },
  {
    title: "标题",
    slotName: "title",
  },
  {
    title: "内容",
    slotName: "content",
  },
  {
    title: "答案",
    slotName: "answer",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "判题用例",
    slotName: "judgeCase",
  },
  {
    title: "判断配置",
    slotName: "judgeConfig",
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
  },
  {
    title: "通过数",
    dataIndex: "acceptedNum",
  },
  {
    title: "用户id",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    title: "修改时间",
    slotName: "updateTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
</script>

<style scoped>
#manageQuestionView {
}
</style>
